<?php

class sfWidgetOptionsTouchscreen extends sfWidgetFormChoiceBase
{
    public function render($name, $value = null, $attributes = array(), $errors = array())
    {
        $result = '<br />'.$this->renderTag('input', array('type' => 'hidden', 'name' => $name, 'value' => $value));
        $choices = $this->getChoices();
        foreach ($choices as $key => $choice) {
        	if($key!=''){
        		$selected = '';
        		if($key == $value){
        			$selected = 'selected';
        		}
            	$result .= '<button group="button'.$this->generateId($name).'" type="button" id="button'.$this->generateId($name).$key.'" class="button_touch ' . $selected .'" onclick="buttonclick'.$this->generateId($name)."('".$key."'" .');">' . $choice . '</button>';
        	}
        }
        
        $result .= sprintf(<<<EOF
<script type="text/javascript">
  function	buttonclick%s(value){
  	jQuery("#%s").val(value);
  	jQuery("[group=button%s]").removeClass('selected');
  	jQuery("#button%s" + value).addClass('selected');
  	%s;
  	%s;
  }
</script>
EOF
      ,
      $this->generateId($name),
      $this->generateId($name),
      $this->generateId($name),
      $this->generateId($name),
      $this->getAttribute('onchange'),
      $this->getAttribute('onChange')
    );
        
    return $result;
    }
}